package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cuser.business.dao.IExamRuleDao;
import com.kayt.cyberBar.cuser.business.entity.po.BExamRule;

@Repository("examRuleDao")
public class ExamRuleDaoImpl implements IExamRuleDao {
	@Autowired
	private EntityDao entityDao;

	@Override
	public Boolean checkExist(String name) throws SQLException {
		String hql = "select count(o.id) as count from BExamRule o where o.examRuleName=?";
        List tempMapList;
        Boolean exist = false;
        tempMapList = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[]{name});
        if(tempMapList != null && !tempMapList.isEmpty()) {
            Map tempMap = (Map)tempMapList.get(0);
            Long tempCount = (Long)tempMap.get("count");
            if (tempCount > 0) {
            	exist = true;
            }
        }
		return exist;
	}
	
	@Override
	public BExamRule findById(String examRuleId) {
		if (StringUtils.isNotEmpty(examRuleId)) {
			Long id = null;
			id = Long.parseLong(examRuleId);
			return entityDao.findById(id, BExamRule.class);
		} else {
			return null;
		}
	}

	@Override
	public Integer delete(String examRuleId) {
		if (StringUtils.isNotEmpty(examRuleId)) {
			Long id = null;
			id = Long.parseLong(examRuleId);
			BExamRule examRule = entityDao.findById(id, BExamRule.class);
			if (examRule != null) {
				entityDao.delete(examRule);
				return 0;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

	@Override
	public void modify(BExamRule entity) throws SQLException {
		entityDao.update(entity);
	}

	@Override
	public PageData<BExamRule> findByPage(String hql, Object[] params, LinkedHashMap<String, String> orderMap)
			throws SQLException {
		PageData<BExamRule> examRulePageData = this.entityDao.getPagingData(BExamRule.class, -1, -1, hql, params,
				orderMap);
		return examRulePageData;
	}

	@Override
	public BExamRule save(BExamRule entity) throws SQLException {
		Long id = entityDao.save(entity);
		entity.setId(id);
		return entity;
	}
	@Override
	public PageData<BExamRule> getExamRuleList(String storeCode,LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<BExamRule> examRulePageData = this.entityDao.getPagingData(BExamRule.class, -1, -1, "o.storeCode = '"+storeCode+"'", null, orderMap);
		return examRulePageData;
	}

	@Override
	public PageData<BExamRule> getExamRulePageByStoreCode(String storeCode, Integer pageNum, Integer pageSize) throws SQLException {
		LinkedHashMap<String, String> orderMap = new LinkedHashMap<>();
		orderMap.put("id", "asc");
		PageData<BExamRule> examRulePageData = this.entityDao.getPagingData(BExamRule.class, pageNum, pageNum, null, null, orderMap);
		return examRulePageData;
	}
}
